from datetime import datetime

class Helpers:
    @classmethod
    def prettydate(self,d):
        try:
            dt = datetime.now() - d
        except:
            return ''
        if dt.days >= 2*365:
            return '%d years ago' % int(dt.days / 365)
        elif dt.days >= 365:
            return '1 year ago'
        elif dt.days >= 60:
            return '%d months ago' % int(dt.days / 30)
        elif dt.days > 21:
            return '1 month ago'
        elif d.timetuple().tm_yday <= datetime.now().timetuple().tm_yday - 6 - 8:
            return '%d weeks ago' % int( (datetime.now().timetuple().tm_yday - d.timetuple().tm_yday ) / 7)
        elif d.timetuple().tm_yday <= datetime.now().timetuple().tm_yday - 6:
            return '1 week ago'
        elif dt.days >= 1:
            return '%d days ago' % dt.days
        elif dt.days == 1:
            return '1 day ago'
        elif dt.seconds >= 2*60*60:
            return '%d hours ago' % int(dt.seconds / 3600)
        elif dt.seconds >= 60*60:
            return '1 hour ago'
        elif dt.seconds >= 2*60:
            return '%d minutes ago' % int(dt.seconds / 60)
        elif dt.seconds >= 60:
            return '1 minute ago'
        elif dt.seconds > 1:
            return '%d seconds ago' % dt.seconds
        elif dt.seconds == 1:
            return '1 second ago'
        else:
            return 'just now'
